import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';

class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});

  @override
  Widget build(BuildContext context) {
    return DefaultTabController(
        length: 2,
        child: Scaffold(
          appBar: AppBar(
            title: Text(this.title),
            actions: <Widget>[
              IconSource("InheritiedWidget","16"),
            ],
          ),
          body: StateDemo(),
        )
    );
  }
}


class CounterProvider extends InheritedWidget{
  final List<String> fruits;
  final VoidCallback onFresh;
  final ValueChanged<String> deleteItem;
  final Widget child;
  CounterProvider({this.fruits,this.onFresh,this.deleteItem,this.child}):super(child:child);

  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    return true;
  }
  static CounterProvider of(BuildContext context){
      return context.inheritFromWidgetOfExactType(CounterProvider);
  }
}

class StateDemo extends StatefulWidget {
  @override
  _StateDemoState createState() => _StateDemoState();
}

class _StateDemoState extends State<StateDemo> {
   List<String> fruits=['apple','oragen','banana'];
   _onFresh(){
    setState(() {
      fruits=['apple','oragen','banana'];
    });
  }
  _deleteItem(String item){
    setState(() {
     fruits.remove(item);
    });
  }
  @override
  Widget build(BuildContext context) {
    return CounterProvider(
      fruits: fruits,
      onFresh: _onFresh,
      deleteItem: _deleteItem,
      child: Scaffold(
        body: Container(
          child: Center(
            child: CounterWrapper(),
          ),
        ),
        floatingActionButton: FloatingActionButton(
          onPressed: _onFresh,
          child: Icon(Icons.refresh),
        ),
      ),
    );
  }
}
class CounterWrapper extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: CounterDemo(),
    );
  }
}

class CounterDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
     List<String> fruits=CounterProvider.of(context).fruits;
     VoidCallback onFresh=CounterProvider.of(context).onFresh;
     ValueChanged<String> deleteItem=CounterProvider.of(context).deleteItem;
    return Center(
      child:Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Wrap(
            spacing: 10.0,
            children: fruits.map((item){
              return Chip(
                label: Text("${item}"),
                onDeleted:(){
                  deleteItem(item);
                },
              );
            }).toList(),
          ),
          Divider(height: 18.0,),
          FlatButton(onPressed: (){
            onFresh();
          }, child: Text("刷新"))
        ],
      ),
    );
  }
}



